package com.aivin.mobileApi;

import com.aivin.entity.*;
import com.aivin.service.PicService;
import com.aivin.service.UserService;
import com.aivin.utils.MyStringUtils;
import com.aivin.utils.MyValidateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  restful设计
 */
@Controller//@Service @Component
@RequestMapping("/mobileApi")
public class ApiController {

    @Autowired
    private PicService picService  ;
    @Autowired
    private UserService userService  ;



    /****
     * 查找最新的图片  。 按照时间排序。    主页图片。
     http://192.168.0.119:8081/findYou/mobileApi/getPicsByTimeDes/0/10
     */
    @RequestMapping(value = "/getPicsByTimeDes/{offset}/{limit}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String  getPicsByTimeDes(   @PathVariable int offset ,@PathVariable int limit) {
        List<PicGroupWithPeopAndLikeInfoBean>  beanList = new ArrayList<PicGroupWithPeopAndLikeInfoBean>()  ;
        List<TbPic>  picList = picService.getAllPicFy( offset  , limit) ;

        for(TbPic picBean :picList){
            PicGroupWithPeopAndLikeInfoBean bean = new PicGroupWithPeopAndLikeInfoBean();
            bean.setPic(picBean);
            bean.setUserBean( getUserBeanInfoByUserId(picBean.getUserId()));
            bean.setPiclikedCount(getPiclikeCountByPicId(picBean.getPicId()));
//            bean.setPiclikedState(getiiclikedStateByPicIdAndUserId(picBean.getPicId() ,picBean.getUserId()));
//            bean.setPiclikedState(100);

            beanList.add(bean);
        }



        String jsonStr ="noData";
        if(!(beanList==null || beanList.size()==0)){
            jsonStr = JSON.toJSONString(beanList );
        }

//        DisableCheckSpecialChar

        return jsonStr;
    }



    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getResultBydKeyWorldFy/2/0/100
     *  根据关键字搜索。
     */
    @RequestMapping(value = "/getResultBydKeyWorldFy/{keyWord}/{offset}/{limit}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String  getResultBydKeyWorldFy(  @PathVariable String  keyWord  ,  @PathVariable int offset ,@PathVariable int limit) {
        System.out.println("keyWord#############################>>>\n\n"+ keyWord);

        PicAndUserGroupBean bean =null ;
        bean = picService.getResultBydKeyWorldFy(keyWord, offset, limit) ;

        String jsonStr ="noData";

        jsonStr = JSON.toJSONString(bean);
       // System.out.print("keyWord>>>"+ keyWord);
        return jsonStr;
    }



    /**
     * 返回给 app 前端的接口。 get方式
     * http://192.168.0.119:8081/findYou/mobileApi/getPicByUserIdFy/1/0/10
     * 根据用户id查找图片。
     */
    @RequestMapping(value = "/getPicByUserIdFy/{userId}/{offset}/{limit}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getPicByUserIdFy(@PathVariable int userId ,  @PathVariable int offset ,@PathVariable int limit  ) {

        PicOfPeopleBean bean  = new PicOfPeopleBean();
        List<TbPic> picList =null ;

        if(userId==0){
            picList = picService.getAllPicFy( offset  , limit) ;
        }else{
            picList = picService.getAllPicByUserId(userId,offset  , limit) ;
        }

        int picSize = picService.getAllPicSizeByUserId(userId);
        bean.setPiclist(picList);
        bean.setPicSize(picSize);
        String jsonStr ="noData";
        jsonStr = JSON.toJSONString(bean);
        return jsonStr;
    }


    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getUserInfoByUserId/2
     *  根据用户id查找用户信息。
     *  已经废弃。
     */
    @Deprecated
    @RequestMapping(value = "/getUserInfoByUserId/{userId}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getUserInfoByUserId(@PathVariable int userId    ) {

        TbUser bean  =   userService.getUserInfoByUserId(userId) ;


        String jsonStr ="noData";
        if(bean!=null){
            jsonStr = JSON.toJSONString(bean);
        }

        return jsonStr;
    }

    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getUserInfoByUserIdV1/32
     *  根据用户id查找用户信息。
     *  新版接口。 替代 getUserInfoByUserId 接口。
     */
    @RequestMapping(value = "/getUserInfoByUserIdV1/{userId}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getUserInfoByUserIdV1(@PathVariable int userId    ) {

        TbUserV1 bean  =   userService.getUserInfoByUserIdV1(userId) ;
        String jsonStr ="noData";
        if(bean!=null){
            jsonStr = JSON.toJSONString(bean);
        }

        return jsonStr;
    }


    @Deprecated
    public TbUser  getUserBeanInfoByUserId(@PathVariable int userId    ) {

        TbUser bean  =   userService.getUserInfoByUserId(userId) ;
        return bean;
    }



//    @RequestMapping(value = "/getPiclikedNumByPicId/{picId}",
//            method = {RequestMethod.POST, RequestMethod.GET},
//            produces = "text/html;charset=UTF-8")
//    @ResponseBody
    public int    getPiclikeCountByPicId (@PathVariable int picId    ) {

       int size  =   picService.getPiclikeCountByPicId(picId) ;
        return size;
    }


    /**
     * 根据 userid和 pic 查看是否被喜欢
     * @param picId
     * @param userId
     * @return
     */
    public int    getiiclikedStateByPicIdAndUserId (@PathVariable int picId   ,@PathVariable int userId  ) {
        System.out.println("123##########"+picId +"   "+userId);
        int size  =   picService.getiiclikedStateByPicIdAndUserId(picId ,userId) ;
        return size;
    }



    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getPicLikedStateByUserId/1/1
     *  根据用户id查询 图片的点赞情况
     * **/
    @RequestMapping(value = "/getPicLikedStateByUserId/{picId}/{userId}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getPicLikedStateByUser(@PathVariable int picId   ,@PathVariable int userId  ){
        int state =picService.getPicLikedStateByUser(picId ,userId) ;

        int likeState =  0 ; //默认为 0状态
        if(state == -1){
           int recodId =  addPicLikedStateByUserId(picId ,userId ,likeState);
            state =  recodId >0 ?  likeState : -1 ;
        }else{
            //..
        }
        return state+"" ;
    }

    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/queryPicLikedListByUserId/2/0/20
     *  用户点赞列表、用户图片收藏列表
     * **/
    @RequestMapping(value = "/queryPicLikedListByUserId/{userId}/{offset}/{limit}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  queryPicLikedListByUserId(@PathVariable int userId   , @PathVariable int offset ,@PathVariable int limit  ){
        List<PiclikedtableBean> likedList  =picService.queryPicLikedListByUserId(userId ,offset ,limit) ;
       String resultStr ="noData" ;
        if(MyValidateUtil.isEmpty(likedList)){
            // ..
        }else{

            List<TbPic> resultList=  new ArrayList<TbPic>();
            for(PiclikedtableBean tempBean : likedList){
                TbPic bean =  picService.queryPicInfoByPicId(tempBean.getPicId());
                resultList.add(bean) ;
            }
            resultStr= JSON.toJSONString(resultList) ;

        }

        return resultStr ;
    }




    /***
     *
     http://192.168.0.119:8081/findYou/mobileApi/setPicLikedStateByUserId/1/1
     */
    @RequestMapping(value = "/setPicLikedStateByUserId/{picId}/{userId}/{likestate}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  setPicLikedStateByUser(@PathVariable int picId   ,@PathVariable int userId  ,@PathVariable int likestate ){

        int state =picService.setPicLikedStateByUser(likestate,picId ,userId) ;
        state = state > 0 ? likestate :  -1 ;
        return state+"" ;

    }


    /*****
     * add  piclikedState record   by userId and picId .
     * used By inner api
     */
    public int   addPicLikedStateByUserId(  int picId ,    int userId  ,  int likeState){
        PiclikedBean bean  = new PiclikedBean() ;
        bean.setUserId(userId);
        bean.setLikestate(likeState);
        bean.setPicId(picId);
        bean.setRegTime(MyStringUtils.getCurrentTimeStr());
        int  recodId =  picService.addPicLikedStateByUserId(bean) ;
        return  recodId   ;
    }






    /***
     * add pic to user
     * http://192.168.0.119:8081/findYou/mobileApi/publishPic
     * 要用@RequestParam("jsonStr")

     */

    @RequestMapping(value = "/publishPic",
            method = {RequestMethod.POST},
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String  publishPic ( @RequestParam("jsonStr") String jsonStr ,@RequestParam("key2") String key2    ){
        TbPic bean = JSON.parseObject(jsonStr, TbPic.class);
        int state= picService.publishPic(bean) ;
        return  state+"";
    }


    /***
     * get some picInfos recommend by system
     * http://192.168.0.119:8081/findYou/mobileApi/getRecommendPics
     */
    @RequestMapping(value = "/getRecommendPics",
            method = {RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getRecommendPics() {

        List<RecommendInfoBean> list = picService.getRecommendPics();
        String jsonStr = "noData";
        if (list == null || list.size() == 0) {
            //to do something if you want...
        } else {
            jsonStr = JSON.toJSONString(list);
        }
        return jsonStr;

    }




}
